package com.example.bluetooth.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.bluetooth.MyApp;
import com.example.bluetooth.R;
import com.example.bluetooth.retrofit.Api;
import com.example.bluetooth.retrofit.Result;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {

    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setTitle("登录");



        Button btnLogin = findViewById(R.id.btn_login);
        EditText etName = findViewById(R.id.et_name);
        EditText etPwd = findViewById(R.id.et_pwd);
        TextView tvGoReg = findViewById(R.id.tv_go_reg);
        context = this;


        SharedPreferences sp = getSharedPreferences("App", MODE_PRIVATE);
        boolean isLogin = sp.getBoolean("isLogin", false);
        if (isLogin) {
            MyApp.map.put("name", sp.getString("name", ""));
            Intent intent = new Intent(context, MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }

        btnLogin.setOnClickListener(v -> {
            Map<String, String> map = new HashMap<>(5);
            map.put("name", etName.getText().toString());
            map.put("password", etPwd.getText().toString());
            RequestBody body = FormBody.create(MediaType.parse("application/json; charset=utf-8"), new Gson().toJson(map));
            //接口对象实例调用相关接口，获得Observable对象
            Api api = MyApp.retrofit.create(Api.class);
            Call<Result> login = api.login(body);
            login.enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    Result res = response.body();
                    if (res.getCode() == 20001) {
                        Toast.makeText(context, res.getMsg(), Toast.LENGTH_SHORT).show();
                    }else if(res.getCode() == 10001){
                        MyApp.map.put("name", etName.getText().toString());
                        SharedPreferences sp = getSharedPreferences("App", MODE_PRIVATE);
                        SharedPreferences.Editor edit = sp.edit();
                        edit.putBoolean("isLogin", true);
                        edit.putString("name", etName.getText().toString());
                        edit.apply();
                        Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(context, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                }

                @Override
                public void onFailure(Call<Result> call, Throwable t) {
                    Toast.makeText(context, "登录失败，请检查网络", Toast.LENGTH_SHORT).show();
                }
            });

        });

        tvGoReg.setOnClickListener(v -> {
            Intent intent = new Intent(context, RegActivity.class);
            startActivity(intent);
            finish();
        });
    }
}
